package cn.schoolwow.ams.module.initial.flow;

import cn.schoolwow.ams.domain.option.QuickAMSListener;
import cn.schoolwow.ams.domain.option.QuickAMSOption;
import cn.schoolwow.ams.module.initial.domain.InitialAMSContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.dto.FlowContextDataRange;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CheckInitialAMSContextExistFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);

        Collection<QuickAMSListener> quickAMSListenerCollection = applicationContext.getBeansOfType(QuickAMSListener.class).values();
        List<QuickAMSListener> quickAMSListenerList = new ArrayList<>(quickAMSListenerCollection);
        QuickAMSOption quickAMSOption = applicationContext.getBean(QuickAMSOption.class);

        flowContext.putTemporaryInstanceData(quickAMSOption);
        flowContext.putContextData("quickAMSListenerList", quickAMSListenerList);

        if(flowContext.containKey("initialAMSContext")&&!quickAMSOption.debug){
            flowContext.broken("初始化已经完成");
        }

        InitialAMSContext initialAMSContext = new InitialAMSContext();
        flowContext.putContextTemporaryInstanceData(initialAMSContext);
    }

    @Override
    public String name() {
        return "检查AMS初始化上下文对象是否存在";
    }
}
